
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入指令：");
            System.out.println("1. 指定关键字检索文件");
            System.out.println("2. 指定后缀名检索文件");
            System.out.println("3. 复制文件/目录");
            System.out.println("4. 删除文件/目录");
            System.out.println("5. 退出");
            String command = scanner.nextLine();

            switch (command) {
                case "1":
                    searchByKeyword();
                    break;
                case "2":
                    searchByExtension();
                    break;
                case "3":
                    copyFileOrDirectory();
                    break;
                case "4":
                    deleteFileOrDirectory();
                    break;
                case "5":
                    System.out.println("退出文件管理器");
                    return;
                default:
                    System.out.println("无效指令，请重新输入");
            }
        }
    }

    private static void searchByKeyword() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入目录：");
        String directory = scanner.nextLine();
        System.out.println("请输入关键字：");
        String keyword = scanner.nextLine();

        try {
            Files.walk(Paths.get(directory))
                    .filter(Files::isRegularFile)
                    .filter(path -> path.getFileName().toString().contains(keyword))
                    .forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void searchByExtension() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入目录：");
        String directory = scanner.nextLine();
        System.out.println("请输入后缀名（用逗号分隔）：");
        String extensions = scanner.nextLine();

        String[] extensionArray = extensions.split(",");
        for (String extension : extensionArray) {
            try {
                Files.walk(Paths.get(directory))
                        .filter(Files::isRegularFile)
                        .filter(path -> path.getFileName().toString().endsWith(extension))
                        .forEach(System.out::println);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void copyFileOrDirectory() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源目录：");
        String source = scanner.nextLine();
        System.out.println("请输入目标目录：");
        String destination = scanner.nextLine();

        Path sourcePath = Paths.get(source);
        Path destinationPath = Paths.get(destination);
        try {
            Files.walk(sourcePath)
                    .forEach(sourceFilePath -> {
                        try {
                            Path relativePath = sourcePath.relativize(sourceFilePath);
                            Path targetPath = destinationPath.resolve(relativePath);
                            Files.copy(sourceFilePath, targetPath);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void deleteFileOrDirectory() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入需要删除的文件目录：");
        String directory = scanner.nextLine();

        File file = new File(directory);
        if (file.exists()) {
            if (file.isDirectory()) {
                deleteDirectory(file);
            } else {
                file.delete();
            }
        }
    }

    private static void deleteDirectory(File directory) {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteDirectory(file);
                } else {
                    file.delete();
                }
            }
        }
        directory.delete();
    }
}
